//////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2014-present, Egret Technology.
//  All rights reserved.
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Egret nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
//  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
//  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////////////
class LoadingUI extends egret.Sprite {

    public constructor() {
        super();
        this.createView();
    }

    private textField:egret.TextField;
    private bmp:egret.Bitmap;
    private sp:egret.Sprite;
    private sp2:egret.Sprite;
    private bmp_bg:egret.Bitmap;
    private sc_bg:egret.SoundChannel;
    private sd_bg:egret.Sound;
    private sc_pos:number=0;
    private bmp_test:egret.Bitmap;
    private msg:Mediator=Mediator.getInstance();
    private deltaBeta:number=0;
    private deltaGamma:number=0;
    private startBeta:number=999;
    private startGamma:number=999;
    private startX;
    private startY;
    private sp_bg:egret.Shape;
     private createView():void {
      this.sp_bg=new egret.Shape();
      this.sp_bg.graphics.beginFill(0,1);
      this.sp_bg.graphics.drawRect(0,0,740,1136);
      this.sp_bg.graphics.endFill();
      this.addChild(this.sp_bg);

       this.bmp=new egret.Bitmap();
       this.bmp.texture=RES.getRes('创邑_LOGO_png');
       this.bmp.name='l';
       this.bmp.anchorOffsetX=this.bmp.width/2;
       this.bmp.x=740/2;
       this.bmp.y=1136/2-130;
       this.addChild(this.bmp);
       
        this.textField = new egret.TextField();
        this.addChild(this.textField);
        this.textField.x = 370;
        this.textField.y = 1136/2+170;
        this.textField.width = 480;
        this.textField.height = 100;
        this.textField.textColor=0xffffff;
        this.textField.anchorOffsetX=this.textField.width/2;
        this.textField.anchorOffsetY=this.textField.height/2;
        this.textField.size=25;
        this.textField.alpha=0.7;
        this.textField.textAlign = "center";

        this.sp=new egret.Sprite();
        this.sp.graphics.beginFill(0xcccccc,1);
        this.sp.graphics.drawRect(0,0,350,2);
        this.sp.anchorOffsetY=this.sp.height/2;
        this.sp.x=(740-this.sp.width)/2;
        this.sp.y=1136/2+100;

        this.sp2=new egret.Sprite();
        this.sp2.graphics.beginFill(0xff0000,1);
        this.sp2.graphics.drawRect(0,0,350,2);
        this.sp2.anchorOffsetX=0;
        this.sp2.anchorOffsetY=this.sp2.height/2;
        this.sp2.x=(740-this.sp2.width)/2;
        this.sp2.y=1136/2+100;
        this.sp2.scaleX=0;

        this.addChild(this.sp);
        this.addChild(this.sp2);

        this.touchEnabled=true;
    }

    
    public setProgress(current:number, total:number):void {
        console.log(current,total);
        // this.textField.text = `Loading...${current}/${total}`;
        this.textField.text=String(Math.floor(100*current/total)+'%');
        this.sp2.scaleX=current/total;
        var myThis=this;
        if(current/total==1)
        {            
            egret.Tween.get(this.bmp).wait(0).to({alpha:0,y:1136/2-200},1000,egret.Ease.cubicInOut);
            egret.Tween.get(this.sp2).wait(0).to({alpha:0,y:1136/2+200},1000,egret.Ease.cubicInOut);
            egret.Tween.get(this.sp).wait(0).to({alpha:0,y:1136/2+200},1000,egret.Ease.cubicInOut);
            egret.Tween.get(this.textField).wait(0).to({alpha:0,y:1136/2+350},1000,egret.Ease.cubicInOut);
            egret.Tween.get(this.sp_bg).wait(500).to({alpha:0},1000,egret.Ease.cubicInOut);
             
            setTimeout(function():void
            {
                myThis.parent.removeChild(myThis);
                // myThis.msg.dispatchEvent(new MediatorEvent('Main_GameIn'));
            },1500);
            
            
        }
    }
    public playMusic():void
    {
          this.sc_bg=this.sd_bg.play(this.sc_pos,0);
    }
     public stopMusic():void
    {
        if(this.sc_bg)
			{
                console.log('stop!!');
				this.sc_pos=this.sc_bg.position;
				this.sc_bg.stop();
				this.sc_bg=null;
			}
    }
}
